package com.tensquare.spit.service.impl;

import com.tensquare.spit.dao.SpitDao;
import com.tensquare.spit.pojo.Spit;
import com.tensquare.spit.service.SpitService;
import entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import util.IdWorker;

import java.util.List;

@Service
public class SpitServiceImpl implements SpitService {
    @Autowired
    private SpitDao spitDao;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private MongoTemplate mongoTemplate;
    /**
     * 1.查询所有的吐槽信息
     * @return 吐槽信息
     */
    @Override
    public List<Spit> findAll() {
        return spitDao.findAll();
    }

    /**
     * 2.查询一个吐槽信息
     * @param spitId    吐槽信息id
     * @return  吐槽信息
     */
    @Override
    public Spit findOne(String spitId) {
        return spitDao.findById(spitId).get();
    }

    /**
     * 3.添加吐槽信息
     * @param spit  要添加的吐槽信息
     */
    @Override
    public void add(Spit spit) {
        spit.set_id(idWorker.nextId() + "");
        spitDao.insert(spit);
    }

    /**
     * 4.修改吐槽信息
     * @param spit  修改的吐槽信息
     */
    @Override
    public void update(Spit spit) {
        spitDao.save(spit);
    }

    /**
     * 5.删除吐槽信息
     * @param spitId    要删除的吐槽信息id
     */
    @Override
    public void delete(String spitId) {
        spitDao.deleteById(spitId);
    }

    /**
     * 6.根据上级ID查询吐槽数据（分页）
     * @param parentid  要查询的上级id
     * @param page  当前页
     * @param size  页大小
     * @return  分页信息
     */
    @Override
    public PageResult<Spit> comment(String parentid, int page, int size) {
        Page<Spit> byParentid = spitDao.findByParentid(parentid, PageRequest.of(page - 1, size));
        return new PageResult<>(byParentid.getTotalElements(),byParentid.getContent());
    }

    /**
     * 7.点赞方法一
     * @param spitId    要点赞的吐槽信息id
     * @return  是否点赞成功的信息
     */
    @Override
    public boolean thumbup(String spitId) {
        //7.1先查询redis中是否有这个点赞信息
        Object o = redisTemplate.opsForValue().get("spit_" + spitId);
        //7.2判断是否点赞过
        if (o != null){
            return false;
        }
        //7.3根据信息id查询吐槽信息
        Spit spit = spitDao.findById(spitId).get();
        //7.4修改点赞次数
        spit.setThumbup(spit.getThumbup() + 1);
        //7.5将其写入到mongodb中
        spitDao.save(spit);
        //7.6将状态写入redis
        redisTemplate.opsForValue().set("spit_" + spitId,"xx");
        return true;
    }

    /**
     * 8.点赞方法二
     * @param spitId    要点赞的吐槽信息id
     * @return  是否点赞成功的信息
     */
    @Override
    public boolean thumbup2(String spitId) {
        //8.1先查询redis中是否有这个点赞信息
        Object o = redisTemplate.opsForValue().get("spit_" + spitId);
        //8.2判断是否点赞过
        if (o != null){
            return false;
        }
        //8.3设置查询条件
        Query query = new Query(Criteria.where("_id").is(spitId));
        //8.4修改点赞数
        Update update = new Update();
        update.inc("thumbup",1);
        //8.5开始点赞
        mongoTemplate.updateFirst(query,update,"spit");
        //8.6将状态写入redis
        redisTemplate.opsForValue().set("spit_" + spitId,"xx");
        return true;
    }


}
